/*The MIT License

Copyright (c) <1/27/2011> <erick.difiore>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

package com.d4a5.mapping.google
{
	import com.d4a5.mapping.core.AmfFactory;
	import com.d4a5.mapping.core.AmfObject;
	import com.d4a5.mapping.core.Bounds;
	import com.d4a5.mapping.core.Utils;
	import com.google.maps.LatLng;
	import com.google.maps.LatLngBounds;
	import com.google.maps.interfaces.IPolyline;
	import com.google.maps.overlays.PolylineOptions;
	import com.google.maps.styles.StrokeStyle;
	
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	import flash.utils.Endian;
	
	public class wmgPolyline extends wmgBaseOverlay implements IPolyline
	{
		protected var _path:Array;
		protected var _polylineOptions:PolylineOptions;
		
		
		public function wmgPolyline( geometry:Object , options:PolylineOptions = null )
		{
			super();
			
			this.setOptions( options );
			
		
			if( geometry is Array ){
				this._path = geometry as Array;
				this._amf = AmfFactory.Create( AmfFactory.formatGoogle , AmfFactory.typePolyline , this._path , this.attributes );
			}
			else if( geometry is AmfObject ){
				this._amf = geometry as AmfObject;
				this.attributes = this._amf.attributes;
			}
		}
		
		override public function set amf( a:AmfObject ) : void {
			super.amf = a;
			this._path = null;
			if( a ){
				this.attributes = a.attributes;
			}
		}
		
		public function getVertex(arg0:Number):LatLng
		{
			return this.path[arg0] as LatLng;
		}
		
		public function isHidden():Boolean
		{
			return !(this.visible);
		}
		
		public function getVertexCount():Number
		{
			return this.path.length;
		}
		
		public function hide():void
		{
			this.visible = false;
		}
		
		public function setOptions(arg0:PolylineOptions):void
		{
			this._polylineOptions = arg0;
		}
		
		public function getOptions():PolylineOptions
		{
			var options:PolylineOptions = this.retrieveOptions() as PolylineOptions;
			
			if( this._polylineOptions ){
				if( options ){
					return PolylineOptions.merge( [ this._polylineOptions , options ] );
				}
				return this._polylineOptions;
			}
			else if( options ){
				return options;
			}
			
			return PolylineOptions.getDefaultOptions();
		}
		
		public function getLatLngBounds():LatLngBounds
		{
			if( this._amf ){
				var b:Bounds = this._amf.bounds;
				var pt:Point = new Point();
				Utils.WebMercatorToGeographicFaster( b.x.min , b.y.min , pt );
				var sw:LatLng = new LatLng( pt.y , pt.x );
				
				Utils.WebMercatorToGeographicFaster( b.x.max , b.y.max , pt );
				var ne:LatLng = new LatLng( pt.y , pt.x );
				
				return new LatLngBounds( sw , ne );
			}
			return null;
		}
		
		public function getLength(arg0:Number=6378137):Number
		{
			throw new Error( "Error: getLength not implemented" );
		}
		
		public function show():void
		{
			this.visible = false;
		}
		
		public function get path():Array {
			if( this._path == null && this._amf ){
				var geographicPt:Point = new Point();
				
				var readCoord:Function = ( this._amf.storedAsFloat ) ? this._amf.geometry.readFloat : this._amf.geometry.readDouble;
				
				this._amf.geometry.position = 0;
				this._amf.geometry.endian = Endian.BIG_ENDIAN;
				
				var outer:Array = new Array();
				var pathsCount:int = this._amf.ringsCount;
				while( pathsCount-- ){
					var inner:Array = new Array();
					var numPoints:int = this._amf.geometry.readInt();
					
					while( numPoints-- ){
						Utils.WebMercatorToGeographicFaster( readCoord() , readCoord() , geographicPt );
						inner.push( new LatLng( geographicPt.y , geographicPt.x ) );
					}
					
					outer.push( inner );
				}
				this._path = outer;
				
			}
			return this._path;
		}
		
		override protected function draw( m:Matrix ):void {
			this.graphics.clear();
			this._drawn = false;
			
			var options:PolylineOptions = this.getOptions();
			var stroke:StrokeStyle = options.strokeStyle;
			
			if( stroke && stroke.alpha > 0 && stroke.thickness > 0 ){
				this.graphics.lineStyle( stroke.thickness as Number, stroke.color as uint , stroke.alpha as Number , stroke.pixelHinting );
				amf.drawRingsOnGraphicsNoTranslation( this.graphics , m );
				this._drawn = true;
				
			}
		}
		
		
	}
}